package com.LeeCode;

/**
 * 替换后的最长重复字符
 */

public class Code424 {
    public static void main(String[] args) {
        String s = "AABABBA";
        int k = 1;
        System.out.println(new Code424().characterReplacement(s, k));
    }

    public int characterReplacement(String s, int k) {
        char[] chars = s.toCharArray();
        int[] cnt = new int[26];
        int ans = 0, left = 0, maxCount = 0;

        for (int right = 0; right < chars.length; right++) {
            cnt[chars[right] - 'A']++;

            // 历史窗口中出现次数最多字母的次数
            maxCount = Math.max(maxCount, cnt[chars[right] - 'A']);
            while (right - left + 1 - maxCount > k) {
                cnt[chars[left] - 'A']--;
                left++;
            }
            ans = Math.max(ans, right - left + 1);
        }
        return ans;
    }
}
